package nix.mybatis;

import java.io.IOException;
import java.io.InputStream;
import java.util.ArrayList;
import java.util.List;

import nix.mybatis.mapper.OrderMapper;
import nix.mybatis.mapper.UserMapper;
import nix.mybatis.pojo.Order;
import nix.mybatis.pojo.QueryVo;
import nix.mybatis.pojo.User;

import org.apache.ibatis.io.Resources;
import org.apache.ibatis.session.SqlSession;
import org.apache.ibatis.session.SqlSessionFactory;
import org.apache.ibatis.session.SqlSessionFactoryBuilder;
import org.junit.Test;

public class MyBatisMapperTest {

	@Test
	public void fun1() throws IOException {
		// 加载核心配置文件
		InputStream in = Resources.getResourceAsStream("SqlMapConfig.xml");
		// 创建SqlSessionFactory
		SqlSessionFactory sqlSessionFactory = new SqlSessionFactoryBuilder().build(in);
		// 创建SqlSession
		SqlSession sqlSession = sqlSessionFactory.openSession();
		// 通过SqlSession获得Mapper接口实现类
		UserMapper userMapper = sqlSession.getMapper(UserMapper.class);
		// 执行sql
		User user = userMapper.findById(10);
		System.out.println(user);
	}
	
	@Test
	public void fun2() throws IOException {
		// 加载核心配置文件
		InputStream in = Resources.getResourceAsStream("SqlMapConfig.xml");
		// 创建SqlSessionFactory
		SqlSessionFactory sqlSessionFactory = new SqlSessionFactoryBuilder().build(in);
		// 创建SqlSession
		SqlSession sqlSession = sqlSessionFactory.openSession();
		// 通过SqlSession获得Mapper接口实现类
		UserMapper userMapper = sqlSession.getMapper(UserMapper.class);
		// 执行sql
		List<User> list = userMapper.findListByUsername("张");
		System.out.println(list);
	}
	
	@Test
	public void fun3() throws IOException {
		// 加载核心配置文件
		InputStream in = Resources.getResourceAsStream("SqlMapConfig.xml");
		// 创建SqlSessionFactory
		SqlSessionFactory sqlSessionFactory = new SqlSessionFactoryBuilder().build(in);
		// 创建SqlSession
		SqlSession sqlSession = sqlSessionFactory.openSession();
		// 通过SqlSession获得Mapper接口实现类
		UserMapper userMapper = sqlSession.getMapper(UserMapper.class);
		// 执行sql
		QueryVo vo = new QueryVo();
		User u = new User();
		u.setUsername("张");
		vo.setUser(u);
		List<User> list = userMapper.findListByQueryVo(vo);
		System.out.println(list);
	}
	
	@Test
	public void fun4() throws IOException {
		// 加载核心配置文件
		InputStream in = Resources.getResourceAsStream("SqlMapConfig.xml");
		// 创建SqlSessionFactory
		SqlSessionFactory sqlSessionFactory = new SqlSessionFactoryBuilder().build(in);
		// 创建SqlSession
		SqlSession sqlSession = sqlSessionFactory.openSession();
		// 通过SqlSession获得Mapper接口实现类
		UserMapper userMapper = sqlSession.getMapper(UserMapper.class);
		// 执行sql
		Integer count = userMapper.getCount();
		System.out.println(count);
	}
	
	@Test
	public void fun5() throws IOException {
		// 加载核心配置文件
		InputStream in = Resources.getResourceAsStream("SqlMapConfig.xml");
		// 创建SqlSessionFactory
		SqlSessionFactory sqlSessionFactory = new SqlSessionFactoryBuilder().build(in);
		// 创建SqlSession
		SqlSession sqlSession = sqlSessionFactory.openSession();
		// 通过SqlSession获得Mapper接口实现类
		OrderMapper orderMapper = sqlSession.getMapper(OrderMapper.class);
		// 执行sql
		List<Order> list = orderMapper.findList(); 
		System.out.println(list);
	}
	
	@Test
	public void fun6() throws IOException {
		// 加载核心配置文件
		InputStream in = Resources.getResourceAsStream("SqlMapConfig.xml");
		// 创建SqlSessionFactory
		SqlSessionFactory sqlSessionFactory = new SqlSessionFactoryBuilder().build(in);
		// 创建SqlSession
		SqlSession sqlSession = sqlSessionFactory.openSession();
		// 通过SqlSession获得Mapper接口实现类
		UserMapper userMapper = sqlSession.getMapper(UserMapper.class);
		// 执行sql
		User u = new User();
		u.setSex("1");
		u.setUsername("张");
		List<User> list = userMapper.findBySexAndUsername(u);
		System.out.println(list);
	}	
	
	@Test
	public void fun7() throws IOException {
		// 加载核心配置文件
		InputStream in = Resources.getResourceAsStream("SqlMapConfig.xml");
		// 创建SqlSessionFactory
		SqlSessionFactory sqlSessionFactory = new SqlSessionFactoryBuilder().build(in);
		// 创建SqlSession
		SqlSession sqlSession = sqlSessionFactory.openSession();
		// 通过SqlSession获得Mapper接口实现类
		UserMapper userMapper = sqlSession.getMapper(UserMapper.class);
		// 执行sql
		Integer[] ids = { 16, 24, 25, 26 };
		List<User> list = userMapper.findByIds(ids);
		System.out.println(list);
	}
	
	@Test
	public void fun8() throws IOException {
		// 加载核心配置文件
		InputStream in = Resources.getResourceAsStream("SqlMapConfig.xml");
		// 创建SqlSessionFactory
		SqlSessionFactory sqlSessionFactory = new SqlSessionFactoryBuilder().build(in);
		// 创建SqlSession
		SqlSession sqlSession = sqlSessionFactory.openSession();
		// 通过SqlSession获得Mapper接口实现类
		UserMapper userMapper = sqlSession.getMapper(UserMapper.class);
		// 执行sql
		List<Integer> ids = new ArrayList<Integer>();
		ids.add(1);
		ids.add(16);
		ids.add(25);
		ids.add(27);
		List<User> list = userMapper.findByIds2(ids);
		System.out.println(list);
	}
	
	@Test
	public void fun9() throws IOException {
		// 加载核心配置文件
		InputStream in = Resources.getResourceAsStream("SqlMapConfig.xml");
		// 创建SqlSessionFactory
		SqlSessionFactory sqlSessionFactory = new SqlSessionFactoryBuilder().build(in);
		// 创建SqlSession
		SqlSession sqlSession = sqlSessionFactory.openSession();
		// 通过SqlSession获得Mapper接口实现类
		UserMapper userMapper = sqlSession.getMapper(UserMapper.class);
		// 执行sql
		QueryVo vo = new QueryVo();
		List<Integer> ids = new ArrayList<Integer>();
		ids.add(1);
		ids.add(10);
		ids.add(16);
		ids.add(24);
		vo.setIdsList(ids);
		List<User> list = userMapper.findByIds3(vo);
		System.out.println(list);
	}
}
